package com.ts.api.interceptor;

import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.module.channel.entity.ChannelRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *  渠道请求对象拦截器
 *
 *  @author Pan
 */
@Component
public class ChannelRequestHandler implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ChannelRequestContext.REQ.set(ChannelRequest.create(request));
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ChannelRequestContext.REQ.remove();
    }
}
